package com.kupurui.asteacher.ui.job;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;

import com.android.frame.util.AppJsonUtil;
import com.android.frame.view.dialog.NiftyDialogBuilder;
import com.android.frame.view.linearlistview.LinearListView;
import com.kupurui.asteacher.R;
import com.kupurui.asteacher.adapter.JobAdapter;
import com.kupurui.asteacher.adapter.YuFaBuAdapter;
import com.kupurui.asteacher.bean.JobInfo;
import com.kupurui.asteacher.bean.NewJobInfo;
import com.kupurui.asteacher.http.Teacher;
import com.kupurui.asteacher.ui.BaseFgt;
import com.kupurui.asteacher.utils.UserManger;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by Administrator on 2017/7/17.
 * 作业
 */

public class JobFgt extends BaseFgt {

    @Bind(R.id.tv_rank)
    TextView tvRank;
    @Bind(R.id.tv_index_title)
    TextView tvIndexTitle;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.listview)
    LinearListView listview;
    @Bind(R.id.yufabu_listview)
    LinearListView yufabuListview;

    NewJobInfo newJobInfo;
    JobAdapter adapter;
    List<NewJobInfo.FabuBean> list;
    YuFaBuAdapter yuFaBuAdapter;
    List<NewJobInfo.YufabuBean> yufabuList;


    NiftyDialogBuilder pushDialog;

    List<String> q_id;

    @Override
    public int getLayoutId() {
        return R.layout.job_fgt;
    }

    @Override
    public void initData() {
        list = new ArrayList<>();
        yufabuList = new ArrayList<>();
        q_id = new ArrayList<>();
        adapter = new JobAdapter(getActivity(),list,R.layout.job_item);
        listview.setAdapter(adapter);

        yuFaBuAdapter = new YuFaBuAdapter(getActivity(),yufabuList,R.layout.yufabu_item);
        yufabuListview.setAdapter(yuFaBuAdapter);

        yufabuListview.setOnItemClickListener(new LinearListView.OnItemClickListener() {
            @Override
            public void onItemClick(LinearListView parent, View view, int position, long id) {
                q_id.clear();
                String[] ss = yufabuList.get(position).getQuestion().split(",");
                for (int i = 0;i<ss.length;i++){
                    q_id.add(ss[i]);
                }
                UserManger.yufabu_id = yufabuList.get(position).getId();
                Bundle bundle = new Bundle();
                bundle.putString("type","1");
                bundle.putStringArrayList("id", (ArrayList<String>) q_id);
                startActivity(PreReleaseAty.class, bundle);
             }
        });

    }

    @Override
    public void requestData() {
        showLoadingContentDialog();
        new Teacher().assignment22(UserManger.getId(),this,0);
    }

    @Override
    public void onResume() {
        super.onResume();
        new Teacher().assignment22(UserManger.getId(),this,0);
    }

    @OnClick({R.id.tv_right})
    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()){
            case R.id.tv_right:
                PushType();
            break;
        }
    }

    @Override
    public void onSuccess(String result, Call call, Response response, int what) {
        switch (what){
            case 0:
                newJobInfo = AppJsonUtil.getObject(result,NewJobInfo.class);
                list.clear();
                list.addAll(newJobInfo.getFabu());
                adapter.notifyDataSetChanged();
                yufabuList.clear();
                yufabuList.addAll(newJobInfo.getYufabu());
                yuFaBuAdapter.notifyDataSetChanged();
                break;
        }
        super.onSuccess(result, call, response, what);
    }

    private void PushType(){
        if (pushDialog == null){
            pushDialog = new NiftyDialogBuilder(getActivity());
            View view = LayoutInflater.from(getActivity()).inflate(R.layout.push_popu,null);
            pushDialog.setND_AddCustomView(view);
            TextView tvPreRelease = (TextView) view.findViewById(R.id.tv_pre_release);
            TextView tvPush = (TextView) view.findViewById(R.id.tv_push);
            tvPreRelease.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    UserManger.PUSH_TYPE = 1;
                    startActivity(JobArrangementAty.class,null);
                    pushDialog.dismiss();
                }
            });
            tvPush.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    UserManger.PUSH_TYPE = 2;
                    startActivity(JobArrangementAty.class,null);
                    pushDialog.dismiss();
                }
            });


            pushDialog.show();
        }else {
            pushDialog.show();
        }
    }

}
